import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '@/api'

Vue.use(Vuex)

const state = {
  token: localStorage.getItem('token') || '',
  roleCode: '',
  userId: '',
  userName: ''
}

const mutations = {
  SETTOKEN (state, token) {
    state.token = token
    localStorage.setItem('token', token)
  }
}

const actions = {
  async asyncLoginAction (store, value) {
    try {
      const res = await loginAPI(value)
      const token = res.token
      store.commit('SETTOKEN', token)
      return Promise.resolve(res) // 返回成功的Promise对象
    } catch (error) {
      return Promise.reject(error) // 返回失败的Promise对象
    }
  }

}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  modules: {
  }
})
